function [er]=geometricnonlinselfcal(x,datapts,usedFs,width, height,scaler)

numFs=size(usedFs,2);

er=0;
scaler=width/15;
for i=1:numFs
    v=distancemine(x,[datapts{i,1}(:,1)' ; datapts{i,1}(:,2)']);
    ce=((v/scaler));

    er=er+ce;
  
   
%     plot(datapts{i,1}(:,1),datapts{i,1}(:,2));
%     hold
%     plot(x(1,1),x(2,1), 'marker','o','markersize',10,'markeredgecolor',[1,0,0],'markerfacecolor',[1,0,0]);
%     title(['distance was ' num2str(v) ' huber dist ' num2str(ce) ]);
%     close all
    

end

er=er/numFs;



end

function [d]= distancemine(pt,v)


SZ=size(v,2);
g=zeros(size(v,2),1);
for j=1:SZ
    g(j,1)=sqrt((v(1,j)-pt(1,1))^2+(v(2,j)-pt(2,1))^2);
end

d=min(g);
end

function w = huber(r)
t = 1.345;
r=r/t;
w = 1 ./ max(1, abs(r));
end